<?php
class Form_Admin_Product extends Zend_Form
{
	private $imageDecorator = array (array ('ViewHelper' ), 
                                    array ('Errors' ), 
                                    array ('Description', array('escape' => false) ), 
                                    array ('Label', array ('separator' => ' ' ) ), 
                                    array ('HtmlTag', array ('tag' => 'p' ) ) ) ;

	private $fileDecorator =  array (array ('File' ), 
                                    array ('Errors' ), 
                                    array ('Description' ), 
                                    array ('Label', array ('separator' => ' ' ) ), 
                                    array ('HtmlTag', 
                                    array ('tag' => 'p' ) ) ) ;

	private $defaultDecorator = array( array('ViewHelper'),
                                array('Errors'),
                                array('Description'),
                                array('Label', array('separator'=>' ')),
                                array('HtmlTag', array('tag' => 'p')));
	
	public $baseURL;
	
	public function init()
	{
		$this->setMethod ( 'post' );
        $this->setAttrib ( 'enctype', 'multipart/form-data' );
        $this->setAttrib ( 'class', 'frmLarge' );
			
		// create new element
		$id = $this->createElement('hidden', 'product_id');
		$id->setDecorators(array('ViewHelper'));
		$this->addElement($id);

		$name = $this->createElement('text', 'name');
		$name->setLabel('Tên sản phẩm: ');
		$name->setRequired(TRUE);
		$name->setAttrib('size',512);
		$name->addFilter('StripTags');
		$name->setDecorators($this->defaultDecorator);
		$name->setOrder(1);
		$this->addElement($name);

		$color = $this->createElement('text', 'color');
		$color->setLabel('Màu sắc: ');
		$color->setRequired(TRUE);
		$color->setAttrib('size',512);
		$color->addFilter('StripTags');
		$color->setDecorators($this->defaultDecorator);
		$color->setOrder(4);
		$this->addElement($color);

		$price = $this->createElement('text', 'price');
		$price->setLabel('Giá: ');
		$price->setRequired(TRUE);
		$price->setAttrib('size',255);
		$price->addFilter('StripTags');
		$price->setDecorators($this->defaultDecorator);
		$price->setOrder(5);
		$price->addValidator('Digits');
		$this->addElement($price);
		
		$quantity = $this->createElement('text', 'quantity');
		$quantity->setLabel('Số lượng: ');
		$quantity->setRequired(TRUE);
		$quantity->setAttrib('size',255);
		$quantity->addFilter('StripTags');
		$quantity->setDecorators($this->defaultDecorator);
		$quantity->setOrder(6);
		$quantity->addValidator('Digits');
		$this->addElement($quantity);
/*
		$description = $this->createElement('textarea', 'description');
		$description->setLabel('Miêu tả: ');
		$description->setRequired(TRUE);
		$description->addFilter('StripTags');
		$description->setAttrib('rows',4);
		$description->setDecorators($this->defaultDecorator);
		$description->setOrder(6);
		$this->addElement($description);
	*/	
		$this->addElement('fck', 'description', array(
		                'label'      => 'Miêu tả',
		                'required'   => true,
		                'order'		 => 7,
		                'editorOptions' => new Zend_Config_Ini(APPLICATION_PATH . '/configs/fck.ini', 'moderator')
		));

		$image = $this->createElement ( 'image', 'show' );
		$image->setLabel ( 'Main Image: ' );
		$image->setAttrib ( 'src', '/public/images/nothumb.jpg' );
		$image->setAttrib ( 'onclick', "return false;" );
		$image->setAttrib ( 'class', "inpType02" );
		$image->setDecorators($this->imageDecorator);
		$image->setOrder(8);
		$this->addElement ( $image );
		
		$attachment = $this->createElement ( 'file', 'main_image' );
		
		$attachment->setLabel ( '.' );
		$attachment->setDescription ( 'Types: jpg,png,gif,jpeg | Size: 630x351' );
		$attachment->setDestination ( 'public/data/products/' );
		$attachment->addValidator ( 'Count', false, 1 );
		$attachment->addValidator ( 'Size', false, "2MB" );
		$attachment->addValidator ( 'Extension', false, 'jpg,png,gif,jpeg' );
		$attachment->setDecorators($this->fileDecorator);
		$attachment->setOrder(9);
		$this->addElement ( $attachment );
		
		
		/*
		* Subs Image
		*/
		for($i = 1; $i < 4; $i ++)
		{
			$image = $this->createElement ( 'image', "image$i" );
			$image->setLabel ( 'Hình phụ '.$i.": " );
			$image->setAttrib ( 'onclick', "return false;" );
			$image->setAttrib ( 'src', '/public/images/nothumb.jpg' );
			$image->setAttrib ( 'class', "inpType02" );
			$image->setDecorators($this->imageDecorator);
			$image->setOrder(10 + $i*2);
			$this->addElement ( $image );
			
			$attachment = $this->createElement ( 'file', 'subImages' . $i );
			$attachment->setLabel ( '.' );
			$attachment->setDescription ( ' ' );
			$attachment->setDestination ( 'public/data/products/' );
			$attachment->addValidator ( 'Count', false, 1 );
			$attachment->addValidator ( 'Size', false, "2MB" );
			$attachment->addValidator ( 'Extension', false, 'jpg,png,gif,jpeg' );
			$attachment->setOrder(11 + $i*2);
			$attachment->setDecorators($this->fileDecorator);
			            $this->addElement ( $attachment );
		}
	}

	public function importCategoryLst($categoryLst)
	{
		$lst = array();
		foreach ($categoryLst as $category)
		{
			$lst[$category['cat_id']] = $category['cat_name'];
		}

		$catID = $this->createElement('select', 'cat_id');
		$catID->setLabel('Danh mục: ');
		$catID->setRequired(TRUE);
		$catID->addMultiOptions($lst);
		$catID->setDecorators($this->defaultDecorator);
		$catID->setOrder(3);
		$this->addElement($catID);
	}

	public function importDimensionLst($dimensionLst)
	{
		$lst = array();
		foreach ($dimensionLst as $dimension)
		{
			$lst[$dimension['dim_id']] = $dimension['dim_value'];
		}

		$dimID = $this->createElement('select', 'dim_id');
		$dimID->setLabel('Tỉ lệ: ');
		$dimID->setRequired(TRUE);
		$dimID->addMultiOptions($lst);
		$dimID->setDecorators($this->defaultDecorator);
		$dimID->setOrder(2);
		$this->addElement($dimID);
	}
	
	public function initalizeImages($baseUrl)
	{
		$this->getElement ( 'show' )->setAttrib ( 'src', $baseUrl . '/public/images/nothumb.jpg' );
		$this->getElement ( 'image1' )->setAttrib ( 'src', $baseUrl . '/public/images/nothumb.jpg' );
		$this->getElement ( 'image2' )->setAttrib ( 'src', $baseUrl . '/public/images/nothumb.jpg' );
		$this->getElement ( 'image3' )->setAttrib ( 'src', $baseUrl . '/public/images/nothumb.jpg' );
	}
	
	public function showImage($baseUrl, $image = null)
	{
		$this->getElement ( 'show' )->setAttrib ( 'src', $baseUrl . '/public/data/products/' . $image );
	}
	
	public function showSubImages($baseUrl, $images)
	{
		foreach($images as $key => $img) 
		{
			$this->getElement ( 'image'.($key+1) )->setAttrib ( 'src', $baseUrl . '/public/data/products/' . $img['img_link'] );
		}
		
	}
	
	public function importSubmitButton()
	{
		$this->addDecorator ( 'FormElements' )
                ->addDecorator ( 'Description' )
                ->addDecorator ( 'Form' );
                
        $submit = $this->createElement ( 'submit', 'submit' );
        
        $submit->setDecorators ( array (array ('ViewHelper' ),
                                //array ('Label' ),  
                                array ('Description' ), 
                                array ('HtmlTag', array ('tag' => 'p' ) ) ) );
        $submit->setOrder(50);
        $this->addElement ( $submit );        
	}

}
?>
